*******************************************************************
* This file generates descriptive statistics on the Cartoon Crisis
* 
* It provides results for Figure 2, Figure A.6 and Figure A.10
*******************************************************************

clear all
set more off

cd "${data_dir}"

*******************************************************************
* Persistent Effects of the Cartoon Crisis on Firm-Level Exports
*******************************************************************
use firmdata_exports_long.dta, clear

gen hs2 = substr(vare,1,2)
tab aar

* Collapse at product-country-year level
collapse (sum) vrd Muslimtradevalue, by(cvrnr aar land vare hs2)

* Indicator for lost relationships:
tab aar
reshape wide vrd Muslimtradevalue, j(aar) i(cvrnr vare land hs2)
reshape long vrd Muslimtradevalue, j(aar) i(cvrnr vare land hs2)


sort cvrnr land vare aar
* Define lost business over 5 years:
gen nextdropped = 0
by cvrnr land vare: replace nextdropped = 1 if vrd != . & vrd[_n+1]==. & aar[_n+1]-1==aar
gen value_nextdropped =vrd*nextdropped

gen next2dropped = 0
by cvrnr land vare: replace next2dropped = 1 if vrd != . & vrd[_n+1]==.  & vrd[_n+2]==. ///
& aar[_n+1]-1==aar & aar[_n+2]-2==aar
gen value_next2dropped =vrd*next2dropped

gen next3dropped = 0
by cvrnr land vare: replace next3dropped = 1 if vrd != . & vrd[_n+1]==.  & vrd[_n+2]==. & vrd[_n+3]==. ///
& aar[_n+1]-1==aar & aar[_n+2]-2==aar & aar[_n+3]-3==aar
gen value_next3dropped =vrd*next3dropped

gen next4dropped = 0
by cvrnr land vare: replace next4dropped = 1 if vrd != . & vrd[_n+1]==.  & vrd[_n+2]==. & vrd[_n+3]==. & vrd[_n+4]==. ///
& aar[_n+1]-1==aar & aar[_n+2]-2==aar & aar[_n+3]-3==aar & aar[_n+4]-4==aar
gen value_next4dropped =vrd*next4dropped

gen next5dropped = 0
by cvrnr land vare: replace next5dropped = 1 if vrd != . & vrd[_n+1]==.  & vrd[_n+2]==. & vrd[_n+3]==. & vrd[_n+4]==. & vrd[_n+5]==. ///
& aar[_n+1]-1==aar & aar[_n+2]-2==aar & aar[_n+3]-3==aar & aar[_n+4]-4==aar & aar[_n+5]-5==aar
gen value_next5dropped =vrd*next5dropped


foreach var in value_nextdropped value_next2dropped value_next3dropped value_next4dropped value_next5dropped {
gen m_`var' = `var' * (Muslimtradevalue>0 & Muslimtradevalue!=.)
}

* collapse data at firm-year:
collapse (sum) vrd Muslimtradevalue value_next* m_*, by(cvrnr aar)

* add information from main analysis data:
merge 1:1 cvrnr aar using hierarchy_cartoon_data, ///
keepusing(sector009 sector027 sector053 sales_usd empl2 treatment sample)
tab aar _merge
drop if _merge == 2
drop _merge
destring aar, replace

gen Muslimdummy = .
replace Muslimdummy = 1 if Muslimtradevalue > 0 & Muslimtradevalue != .
replace Muslimdummy = 0 if vrd > 0 & vrd != . & Muslimtradevalue == 0

* define 2005 treatment:
gen Arab2005help = (Muslimdummy==1) * (aar==2005) if aar==2005 & Muslimdummy != .
bys cvrnr: egen Arab2005 = max(Arab2005help)

gen Arabshare2005help = Muslimtradevalue/vrd if aar == 2005
bys cvrnr: egen Arabshare2005 = max(Arabshare2005help)

gen treatment2005 = .
replace treatment2005 = 1 if Arab2005 == 1 & Arabshare2005 > 0.005
replace treatment2005 = 0 if Arab2005 == 0
tab aar treatment2005

* extend the "sample" definition beyond 2008
sort cvrnr aar
by cvrnr: replace sample = sample[_n-1] if aar >= 2009

* extend the treatment definition beyond 2008
sort cvrnr aar
by cvrnr: egen maxtreatment = max(treatment)
replace treatment = maxtreatment if treatment == .

***********************************
* Time Series Evidence: Raw Data
***********************************
cd "${results_dir}"

set more off
preserve
gen treatmentmuslimtotal = treatment * Muslimtradevalue
collapse (sum) Muslimtradevalue treatmentmuslimtotal, by(aar)

foreach var in Muslimtradevalue treatmentmuslimtotal {
replace `var' = `var'/(1000000*8.09)
}

rename aar year
keep if year > 1999 & year < 2016

line Muslimtradevalue year, lpattern(shortdash) lcolor(black) ///
|| line treatmentmuslimtotal year, lpattern(solid) lcolor(black) ///
graphregion(color(white)) ///
xline(2005.5, lwidth(8) lc(gs14)) ///
ytitle("Exports [in millions of USD]") ///
xscale(range(2000(2)2008)) ///
legend(label(1 "Total Exports") ///
label(2 "Treatment Group")) ///
title(Danish Exports to Muslim Countries)
graph export Figure2_timeseries.pdf, replace

restore

*******************************
* Prepare regressions:
*******************************
forvalues x = 2000/2016 {
cap drop treatment2005_`x'
gen treatment2005_`x' = treatment2005 * (aar == `x')
}

gen value1dropped = value_nextdropped / vrd
forvalues x = 2/5{
gen value`x'dropped = value_next`x'dropped / vrd
}

* extrapolate sector for 2011 onwards:
sort cvrnr aar
by cvrnr: replace sector053 = sector053[_n-1] if aar >= 2011

egen firmcode = group(cvrnr)
xtset firmcode aar


*************************************************************************
* Shock Persistence over 5 years
*************************************************************************

set more off
foreach var in treatment2005 {
forvalues x = 1/5 {

areg value`x'dropped `var'_2000-`var'_2003 `var'_2005-`var'_2012 ///
i.aar i.sector053 ///
if aar >=2000 & aar <= 2012 & sample==1, absorb(firmcode) cluster(sector053)

local firms = `e(df_a)' + 1

* average share of value dropped in the base year 2004 for the treatment group:
quietly sum value`x'dropped if aar == 2004 & sample == 1 & `var' == 1
local preval = r(mean)
quietly sum value`x'dropped if aar >= 2000 & aar <= 2004 & sample == 1 & `var' == 1
local preval2 = r(mean)

outreg2 using Table_persistence_`var'.tex, tex alpha(0.01, 0.05, 0.1) ///
symbol(***,**,*) bdec(4) nocons adds(Firms, `firms', 2004 Average, `preval', "2000-2004 Average", `preval2') ///
keep(`var'_2000-`var'_2003 `var'_2005-`var'_2012) append

estimates save "estfe_`var'_y`x'", replace

}
}

set more off
foreach var in treatment2005 {

preserve

forvalues x = 1/5 {
clear
estimates use "estfe_`var'_y`x'"

matrix define lambda_y`x'_b = e(b)
matrix define lambda_y`x'_se = e(V)

matrix lambda_y`x'_se2 = vecdiag(lambda_y`x'_se)

matrix param_y`x'_b = [lambda_y`x'_b[1,1..12]]'
matrix param_y`x'_se = [lambda_y`x'_se2[1,1..12]]'

}

forvalues x = 1/5 {
svmat param_y`x'_b
svmat param_y`x'_se
}


gen year = _n
replace year = year + 1999 if _n<=4
replace year = year + 2000 if _n>4
drop if year >= 2013

expand 2 in 4

sort year

forvalues x = 1/5 {
replace param_y`x'_b = 0 if year == 2003 & year[_n-1]==2003
replace param_y`x'_se = 0 if year == 2003 & year[_n-1]==2003
}
replace year = 2004 if year == 2003 & year[_n-1]==2003
sort year
forvalues x = 1/5 {
replace param_y`x'_se = sqrt(param_y`x'_se)
gen ci_y`x'_up = param_y`x'_b + (param_y`x'_se*1.64)
gen ci_y`x'_low = param_y`x'_b - (param_y`x'_se*1.64)
}

gen year_y2 = year+ 0.1
gen year_y3 = year+ 0.2
gen year_y4 = year+ 0.3
gen year_y5 = year+ 0.4

twoway (scatter param_y1_b year, connect(l) lpattern(solid) lcolor(gs6) mcolor(gs6)) ///
(rcap ci_y1_up ci_y1_low year,  lpattern(solid) lcolor(gs6)) ///
 (scatter param_y2_b year_y2, connect(l) lpattern(longdash) lcolor(gs6) mcolor(gs6)) ///
(rcap ci_y2_up ci_y2_low year_y2, lpattern(longdash) lcolor(gs6)) ///
 (scatter param_y3_b year_y3, connect(l) lpattern(solid) lcolor(gs10) mcolor(gs10)) ///
(rcap ci_y3_up ci_y3_low year_y3, lpattern(solid) lcolor(gs10)) ///
 (scatter param_y4_b year_y4, connect(l) lpattern(longdash) lcolor(gs10) mcolor(gs10)) ///
(rcap ci_y4_up ci_y4_low year_y4, lpattern(longdash) lcolor(gs10)) ///
 (scatter param_y5_b year_y5 if year <= 2011, connect(l) lpattern(solid) lcolor(gs12) mcolor(gs12)) ///
(rcap ci_y5_up ci_y5_low year_y5 if year <= 2011, lpattern(longdash) lcolor(gs12)), ///
yline(0, lpattern(dash) lcolor(black)) ///
legend(order(1 - " " 3 5 7 9) label(1 "Product-Destinations Lost Next Year") label(3 "Lost for 2 Years") ///
label(5 "Lost for 3 Years") label(7 "Lost for 4 Years") label(9 "Lost for 5 Years")) ///
xtitle(Year) graphregion(color(white)) xscale(range(2000(1)2012)) xlabel(2000(5)2010) ///
title("Discontinued Exports") subtitle("Within Firm") ytitle(Share of Total Exports)

graph export FigureA6_eventstudy.pdf, replace

restore

}

* 


*************************************************************************
* Figure 2B
*************************************************************************

preserve
keep if aar == 2005 & treatment != .

* Analysis by sector:
gen relsector = sector027
replace relsector = 29 if sector053 == 2900
replace relsector = 30 if sector053 == 3000
replace relsector = 60 if sector053 == 3500
replace relsector = 72 if sector027 == 64 | sector027 == 70
replace relsector = 51 if sector027 == 52

tab relsector

drop if relsector == 55 | relsector == 10 | relsector==20 | relsector == 40 ///
 | relsector == 65 | relsector > 72 | relsector == 1 | relsector == 5 

label define industry27label ///
10 "Mining" ///
15 "Food, Beverages, Tobacco" ///
17 "Textiles and Leather" ///
20 "Wood Products, Printing" ///
23 "Chemicals and Plastics" ///
26 "Mineral Products" ///
27 "Basic Metals" ///
29 "Machinery and Equipment" ///
30 "Electronic Components" ///
36 "Furniture and Toys" ///
45 "Construction" ///
50 "Sale of Motor Vehicles" ///
51 "Wholesale and Retail Trade" ///
55 "Hotels and Restaurants" ///
60 "Transport" ///
72 "Business Activities"

label values relsector industry27label

bys relsector: egen shockshare = mean(treatment)

gen shareexp = Muslimtradevalue / vrd
gen shareexp_miss = shareexp if treatment == 1

graph hbar (mean) shareexp_miss (median) shareexp_miss (mean) treatment, ///
over(relsector, sort(shockshare) reverse label(labsize(small))) ///
graphregion(color(white)) ylabel(, labsize(small)) ///
ytitle(Share of Exposed Firms and Their Share of Exposed Exports, size(small)) ///
legend(order(3 2 1) col(3) label(3 "Share of Firms") ///
label(2 "Median Export Share") label(1 "Average Export Share") ///
ring(1) position(5) size(*0.7) symxsize(*0.5) colgap(*0.5) keygap(*0.5)) ///
title("Cartoon Crisis by Industry") subtitle(Exposure in 2005) ///
bar(1, color(gs12)) bar(2, color(gs8)) bar(3, color(gs2))
graph export "Figure2_industryexposure.pdf", replace


* average share of lost business in Muslim countries:
collapse (sum) m_value_next* Muslimtradevalue, by(relsector)

* value of lost business in Muslim countries as share of total in Muslim countries
gen msharelost_t1 =  m_value_nextdropped / Muslimtradevalue 
forvalues x = 2/5 {
gen msharelost_t`x' =  m_value_next`x'dropped / Muslimtradevalue
}

bys relsector: egen mshockshare = mean(msharelost_t1)

graph hbar (mean) msharelost_t5 (mean) msharelost_t3 (mean) msharelost_t1, ///
over(relsector, sort(mshockshare) reverse) bar(1, color(gs12)) ///
bar(2, color(gs8)) bar(3, color(gs2)) ///
graphregion(color(white)) ytitle(Share of Lost Business after 2005) ///
legend(col(1) order(3 2 1) label(3 "Lost in 2006") label(2 "Lost 2006-2008") ///
label(1 "Lost 2006-2010") ring(0) position(4) size(*0.75)) ///
title("Cartoon Crisis by Industry") ///
subtitle(Share of Lost Business to Muslim Countries) 
graph export "FigureA6_bargraph.pdf", replace

restore


*************************************
* Descriptives: Imports From China
*************************************
clear all
set more off
cd "${data_dir}"
use firmdata_imports_long.dta, clear


cd "${results_dir}"

preserve
gen china = (land=="CN")

collapse (sum) vrd, by(aar china)

replace china = china + 1
reshape wide vrd, j(china) i(aar)
gen sharechina = vrd2 / (vrd1+vrd2)

scatter sharechina aar if aar >1998 & aar <= 2010, ///
title("Imports from China") ytitle("Import Share of Total") legend(label(1 "Total for Denmark")) ///
xtitle(Year) graphregion(color(white))

graph export FigureA10_aggregate.pdf, replace
restore

cd "${data_dir}"
merge n:1 cvrnr aar using hierarchy_cartoon_data.dta, keepusing(treatment sample)

tab aar _merge
drop if _merge == 2
drop _merge

cd "${results_dir}"

preserve
keep if treatment != . & sample == 1
gen vrd_china = vrd if land=="CN"

collapse (sum) vrd vrd_china, by(aar cvrnr treatment)

gen sharechina = vrd_china / vrd

collapse (mean) sharechina, by(aar treatment)

scatter sharechina aar if treatment == 0 ///
|| scatter sharechina aar if treatment == 1, ///
legend(label(1 "Control Firms") label(2 "Treatment Firms")) ///
title("Imports from China") ytitle("Share of Total Imports") ///
xtitle(Year) graphregion(color(white))

graph export FigureA10_treatment.pdf, replace
restore


